/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Diagnostics;

namespace Xceed.Silverlight.Data
{
  [DebuggerDisplay( "DummyPosition<T>( UseBaseItem={m_useBaseItem}, BaseItem={m_baseItem}, BasePosition={m_baseSourcePosition}, Offset={m_baseOffset}, RealPosition={m_realPosition} )" )]
  internal class DummyPosition<T> : SourcePosition
  {
    internal DummyPosition( SourcePosition position, long positionOffset, bool approximateOffset )
    {
      if( position == null )
        throw new ArgumentNullException( "position" );

      if( position is ItemPosition )
      {
        if( !( ( ( ItemPosition )position ).Item is T ) )
          throw new InvalidOperationException( "The ItemPosition must contain a valid item of the generic type." );
      }

      m_useBaseItem = false;
      m_baseSourcePosition = position;
      m_baseOffset = positionOffset;
      m_approximateOffset = approximateOffset;
    }

    internal DummyPosition( T baseItem )
      : this( baseItem, 0, false )
    {
    }

    internal DummyPosition( T baseItem, long positionOffset, bool approximateOffset )
    {
      m_useBaseItem = true;
      m_baseItem = baseItem;
      m_baseOffset = positionOffset;
      m_approximateOffset = approximateOffset;
    }

    public bool BasedOnItem
    {
      get
      {
        return m_useBaseItem;
      }
    }

    public T BaseItem
    {
      get
      {
        if( !this.BasedOnItem )
          throw new InvalidOperationException( "The position is not based on an item." );

        return m_baseItem;
      }
    }

    public SourcePosition BaseSourcePosition
    {
      get
      {
        if( this.BasedOnItem )
          throw new InvalidOperationException( "The position is not based on a source position." );

        return m_baseSourcePosition;
      }
    }

    public long BaseOffset
    {
      get
      {
        return m_baseOffset;
      }
    }

    public bool BaseApproximateOffset
    {
      get
      {
        return m_approximateOffset;
      }
    }

    #region RealPosition Property

    internal SourcePosition RealPosition
    {
      get
      {
        return m_realPosition;
      }
      set
      {
        if( value == null )
          throw new ArgumentNullException( "RealPosition" );

        Debug.Assert( m_realPosition == null );
        Debug.Assert( !( value is DummyPosition<T> ) );

        m_realPosition = value;
      }
    }

    #endregion RealPosition Property


#if DEBUG
    public override string ToString()
    {
      object baseData;
      if( m_useBaseItem )
      {
        baseData = m_baseItem;
      }
      else
      {
        baseData = m_baseSourcePosition;
      }

      string approxStr = (m_approximateOffset) ? " approx" : "";

      return string.Format( "DummyPos(base={0}, offset={1}{2})", baseData, m_baseOffset, approxStr );
    }
#endif


    #region Private Fields

    private bool m_useBaseItem; //false
    private T m_baseItem; //null
    private SourcePosition m_baseSourcePosition;
    private long m_baseOffset; //0
    private bool m_approximateOffset;
    private SourcePosition m_realPosition;

    #endregion Private Fields
  }
}
